package org.example.crawler.service;


import org.apache.poi.ss.usermodel.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExcelService {

    public Map<String, List<String>> readKeywordsFromExcel() {
        Map<String, List<String>> keywordMap = new HashMap<>();

        try {
            ClassPathResource resource = new ClassPathResource("keywords.xlsx");
            InputStream inputStream = resource.getInputStream();

            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                String key = row.getCell(0).getStringCellValue();
                List<String> keywords = new ArrayList<>();

                for (int i = 1; i < row.getLastCellNum(); i++) {
                    Cell cell = row.getCell(i);
                    if (cell != null) {
                        keywords.add(cell.getStringCellValue());
                    }
                }

                keywordMap.put(key, keywords);
            }

            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return keywordMap;
    }
}
